var w = Object.defineProperty;
var E = (r, s, t) => s in r ? w(r, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[s] = t;
var o = (r, s, t) => (E(r, typeof s != "symbol" ? s + "" : s, t), t);
const c = "star_audio_list", y = `${c}_list_mode`, p = `${c}_player_list`, m = `${c}_current_index`;
var S = /* @__PURE__ */ ((r) => (r.LOOP = "loop", r.SINGLE = "single", r.ORDER = "order", r.RANDOM = "random", r.NONE = "none", r))(S || {}), b = /* @__PURE__ */ ((r) => (r.AUDIO = "audio", r.BAN_ZOU = "banZou", r))(b || {});
function P(r, s) {
  return s.split(".").reduce((t, e) => t && t[e], r);
}
function T(r, s) {
  return Math.floor(Math.random() * (s - r + 1)) + r;
}
function I(r) {
  const s = Math.floor(r / 3600), t = Math.floor((r - s * 3600) / 60), e = Math.floor(r - s * 3600 - t * 60), n = s < 10 ? "0" + s : s, i = t < 10 ? "0" + t : t, h = e < 10 ? "0" + e : e;
  return `${n}:${i}:${h}`;
}
const f = Symbol("create"), a = class a extends Audio {
  constructor(t) {
    if (t !== f)
      throw new Error("请使用 create 静态方法创建实例！");
    super();
    /**
     * 当前播放的索引
     */
    o(this, "_currentIndex", 0);
    /**
     * 关闭，请使用方法 setCurrentIndex
     */
    // set currentIndex(index: number) {
    //   const isPlaying = !this.paused;
    //   const currentSrc = this.playerList[index].src;
    //   if (decodeURI(this.src) === currentSrc) {
    //     return;
    //   }
    //   this.src = this.playerList[index].src;
    //   this._currentIndex = index;
    //   localStorage.setItem(CURRENT_INDEX_LOCAL_NAME, index + "");
    //   if (isPlaying) {
    //     this.play();
    //   }
    //   this.trigreeSwitchSongs();
    // }
    o(this, "originSrc");
    o(this, "_playerList", []);
    o(this, "handleUrl");
  }
  addEventListener(t, e, n) {
    super.addEventListener(t, e, n);
  }
  removeEventListener(t, e, n) {
    super.removeEventListener(t, e, n);
  }
  /**
   * 播放模式
   */
  get mode() {
    return localStorage.getItem(y) || "single";
  }
  set mode(t) {
    localStorage.setItem(y, t), this.triggerChangeMode();
  }
  /**
   * 变化播放模式
   * @param mode
   */
  changeMode(t) {
    if (t)
      this.mode = t;
    else {
      const n = (a.modes.findIndex((i) => i === this.mode) + 1) % a.modes.length;
      this.mode = a.modes[n];
    }
  }
  /**
   * 切换音频类型
   * audio 切换主音频
   * banZou 切换伴奏音频
   * @param type
   */
  changeAudioType(t) {
    this.setCurrentIndex({ index: this.currentIndex, audioType: t }), this.dispatchEvent(new CustomEvent("changeAudioType", { detail: t }));
  }
  triggerChangeMode() {
    this.dispatchEvent(new CustomEvent("modeChage", { detail: this.mode }));
  }
  /**
   * 在切换音频之前要做的事情。可以重写这个函数，可以根据回调中的参数去获取音乐详情。或做一些其他的事情。
   * @param payload IplayerList
   * @returns
   */
  static async handleChangeIndexAftre(t) {
    return t;
  }
  async setCurrentIndex({
    index: t,
    audioType: e = "audio"
    /* AUDIO */
  } = {
    index: this.currentIndex
  }) {
    var l, u;
    let n;
    t === this.currentIndex && e === "banZou" && ((l = this.currentPlayerInfo) != null && l.src || (u = this.currentPlayerInfo) != null && u.banZouUrl) ? n = this.currentPlayerInfo : n = await a.handleChangeIndexAftre(this.playerList[t]);
    let i = n.src, h = this.currentTime || 0;
    switch (e) {
      case "banZou":
        i = n.banZouUrl || i;
        break;
    }
    if (this.originSrc === i)
      return;
    const L = !this.paused;
    let d = i;
    try {
      this.handleUrl && (d = await this.handleUrl(d)), this.src = d;
    } catch {
      this.src = d;
    }
    const C = { ...this.currentPlayerInfo };
    this.playerList = this.playerList.map((g, v) => t == v ? n : g), this.originSrc = i, this._currentIndex = t, localStorage.setItem(m, t + ""), L && await this.play(), C.id === this.currentPlayerInfo.id && (this.currentTime = h), this.trigreeSwitchSongs();
  }
  get currentIndex() {
    let t = localStorage.getItem(m);
    return t ? parseInt(t) : this._currentIndex;
  }
  get currentPlayerInfo() {
    return this.playerList[this.currentIndex];
  }
  /**
   * 创建播放器
   * @returns
   */
  static create(t) {
    if (!a.audioList) {
      const e = new a(f);
      a.audioList = e, e.init(t);
    }
    return a.audioList;
  }
  /**
   * 播放器列表
   */
  get playerList() {
    const t = localStorage.getItem(p);
    return t ? JSON.parse(t) : this._playerList;
  }
  set playerList(t) {
    this._playerList = t, localStorage.setItem(p, JSON.stringify(t));
  }
  /**
   * 设置播放列表
   * @param playerList
   * @param index 设置要播放的索引 默认是 0
   */
  async setPlayerList(t, e = 0) {
    this.playerList = t, await this.setCurrentIndex({ index: e }), this.trigreePlayerListUpdate();
  }
  /**
   * 获取当前播放列表
   * @returns
   */
  getPlayerList() {
    return this.playerList;
  }
  /**
   * 上一首播放
   */
  async prev(t) {
    const e = this.currentIndex - 1;
    if (e < 0) {
      const n = this.playerList.length;
      await this.setCurrentIndex({ index: n - 1 });
    } else
      await this.setCurrentIndex({ index: e });
    return t && await this.play(), this.currentPlayerInfo;
  }
  /**
   * 下一首
   */
  async next(t) {
    const e = this.playerList.length, n = this.currentIndex + 1;
    return n === e ? await this.setCurrentIndex({ index: 0 }) : await this.setCurrentIndex({ index: n }), t && await this.play(), this.currentPlayerInfo;
  }
  async randomPlay(t) {
    const e = this.playerList.length, n = T(0, e - 1);
    return await this.setCurrentIndex({ index: n }), t && await this.play(), this.currentPlayerInfo;
  }
  init({ handleUrl: t } = {}) {
    this.handleUrl = t, this.addEventListener("ended", this.onEnded.bind(this)), this.addEventListener("timeupdate", this.onCustomTimeupdate.bind(this)), this.addEventListener("error", this.onError.bind(this)), setTimeout(() => {
      this.playerList.length > 0 && this.setPlayerList(this.playerList, this.currentIndex), this.triggerChangeMode();
    }, 1e3);
  }
  /**
   * 播放完成事件
   * @param event
   */
  onEnded() {
    switch (this.mode) {
      case "loop":
        this.next(!0);
        break;
      case "single":
        this.play();
        break;
      case "order":
        if (this.playerList.length - 1 === this.currentIndex)
          return;
        this.next(!0);
        break;
      case "random":
        this.randomPlay(!0);
        break;
    }
  }
  trigreeSwitchSongs() {
    this.dispatchEvent(
      new CustomEvent("switchSongs", {
        detail: {
          info: this.currentPlayerInfo,
          currentIndex: this.currentIndex
        }
      })
    );
  }
  onCustomTimeupdate() {
    const t = new CustomEvent("videoListTimeupdate", {
      detail: {
        progress: this.progress,
        type: "videoListTimeupdate",
        formatDuration: I(this.duration || 0),
        formatCurrentTime: I(this.currentTime || 0),
        duration: this.duration,
        currentTime: this.currentIndex
      }
    });
    this.dispatchEvent(t);
  }
  /**
   * 播放进度百分比
   */
  get progress() {
    return this.duration ? Math.floor(this.currentTime / this.duration * 100) : 0;
  }
  set progress(t) {
    const e = this.duration;
    e && (this.currentTime = t / 100 * e);
  }
  /**
   * 将不符合 IplayerList 接口规则的对象，转换成符合接口规则的对象。
   * @param item 原始对象
   * @param transformMap 要映身的字段
   * @returns
   * @example
   * AudioList.transformPlayerList(item, {
                  title: "mulu",
                  src: "dmtUrl.adUrl",
                  serial: "xuhao",
                })
   * 
   * 
   */
  static transformPlayerList(t, e) {
    let n = {};
    for (let i in e)
      n[i] = P(t, e[i]);
    return Object.assign(t, n);
  }
  onError() {
    console.log("播放失败", document.hidden), document.hidden && this.onEnded();
  }
  trigreePlayerListUpdate() {
    const t = new CustomEvent("playerListUpdate", {
      detail: this.playerList
    });
    this.dispatchEvent(t);
  }
};
o(a, "modes", ["single", "order", "random", "loop", "none"]), o(a, "audioList");
let x = a;
export {
  S as PLAYMODEENUM,
  b as PLAY_AUDIO_TYPE_ENUM,
  x as default
};
